home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 317 / asmsrc / gdb-bloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  7.4 KB  |  292 lines

  1. /* gdb_block.c - */
  2.  
  3. /* Copyright (C) 1987 Free Software Foundation, Inc.
  4.  
  5. This file is part of Gas, the GNU Assembler.
  6.  
  7. The GNU assembler is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the GNU Assembler General
  12. Public License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. the GNU Assembler, but only under the conditions described in the
  16. GNU Assembler General Public License.  A copy of this license is
  17. supposed to have been given to you along with the GNU Assembler
  18. so you can know your rights and responsibilities.  It should be
  19. in a file named COPYING.  Among other things, the copyright
  20. notice and this notice must be preserved on all copies.  */
  21.  
  22. /*
  23.  * Implements .gdbblk, .gdbbeg, .gdbend concepts.
  24.  * No other modules need to know the details of these concepts.
  25.  *
  26.  * During assembly, note the addresses of block beginnings and ends.
  27.  * Each block has a begin-address, an end-address, a number, and
  28.  * a place in the GDB symbol file to place the 2 addresses.
  29.  * Block numbers are 0, 1, ... with no gaps.
  30.  *
  31.  * During assembly, we don't actually know the addresses, so they are
  32.  * expressed as {frag-address + offset-in-frag}.
  33.  *
  34.  * gdb_block_begin ()
  35.  *        Call once before using this package.
  36.  *
  37.  * gdb_block_beg  (number, frag, offset)
  38.  *        Note a block beginning.
  39.  *
  40.  * gdb_block_end    (number, frag, offset)
  41.  *        Note a block end.
  42.  *
  43.  * gdb_block_position (block_number, pos)
  44.  *        Remember, after assembly, to copy a structure containing
  45.  *        the beginning and ending addresses of block number
  46.  *        block_number into the gdb file, starting at position pos.
  47.  *
  48.  * gdb_block_emit  (block_number, where_in_gdb_symbol_file)
  49.  *        Emit a block begin/end locations to a place in the GDB symbol
  50.  *        file.
  51.  *
  52.  * uses:
  53.  *    xmalloc()
  54.  *    gdb_alter()
  55.  */
  56.  
  57.  
  58. #include "as.h"
  59.  
  60. /*
  61.  * malloc() calls are considered expensive. So ...
  62.  *
  63.  * We remember blocks by making a tree, and each block number has a leaf.
  64.  * The tree is 3 levels, and we don't allocate interior nodes until they
  65.  * are needed. Both leaves and interior nodes are allocated in lumps,
  66.  * which should save on malloc() calls. Due to the way we break up a
  67.  * block number to navigate through the tree, we insist that lumps of
  68.  * memory contain a power of 2 items each. Powers of 2 may differ
  69.  * for different levels of tree.
  70.  */
  71.  
  72. /*
  73.  *    A block number:
  74.  *
  75.  *    +---------------+---------------+---------------+
  76.  *    |        |        |        |
  77.  *    |  Z2-part bits    |  Z1-part bits    |  Z0-part bits    |
  78.  *    |        |        |        |
  79.  *    +---------------+---------------+---------------+
  80.  *
  81.  *    High order                Low order
  82.  *
  83.  * "Z" is short for "siZe".
  84.  */
  85.  
  86. #define LOG_2_Z0 (8)        /* How many bits are in Z0 part? */
  87. #define LOG_2_Z1 (8)        /* How many bits are in Z1 part? */
  88. #define LOG_2_Z2 (8)        /* How many bits are in Z2 part? */
  89.  
  90. #define BLOCK_NUMBER_LIMIT (1 << (LOG_2_Z0 + LOG_2_Z1 + LOG_2_Z2))
  91.                 /* What is the first block number that is */
  92.                 /* "too big"? */
  93.  
  94. struct gdb_block
  95. {
  96.   fragS *    begin_frag;
  97.   fragS *      end_frag;
  98.   long int    begin_where_in_frag;
  99.   long int      end_where_in_frag;
  100.   long int    position;    /* In GDB symbols file. */
  101. };
  102.  
  103. typedef struct gdb_block    node_0_T    [1 << LOG_2_Z0];
  104.  
  105. typedef node_0_T *        node_1_T    [1 << LOG_2_Z1];
  106.  
  107. typedef node_1_T *        node_2_T    [1 << LOG_2_Z2];
  108.  
  109.  
  110. static long int        highest_block_number_seen;
  111. static node_2_T *    root;    /* 3 level tree of block locations. */
  112.  
  113.  
  114. char * xmalloc();
  115. void gdb_alter();
  116.  
  117. void
  118. gdb_block_begin ()
  119. {
  120.   node_2_T *    new_2 ();
  121.  
  122.   root = new_2 ();
  123.   highest_block_number_seen = -1;
  124. }
  125.  
  126. static node_0_T *
  127. new_0 ()
  128. {
  129.   register node_0_T *    place;
  130.  
  131.   place = (node_0_T *) xmalloc ((long)sizeof(node_0_T));
  132.   bzero ((char *)place, sizeof(node_0_T));
  133.   return (place);
  134. }
  135.  
  136. static node_1_T *
  137. new_1 ()
  138. {
  139.   register node_1_T *    place;
  140.  
  141.   place = (node_1_T *) xmalloc ((long)sizeof(node_1_T));
  142.   bzero ((char *)place, sizeof(node_1_T));
  143.   return (place);
  144. }
  145.  
  146. static node_2_T *
  147. new_2 ()
  148. {
  149.   register node_2_T *    place;
  150.  
  151.   place = (node_2_T *) xmalloc ((long)sizeof(node_2_T));
  152.   bzero ((char *)place, sizeof(node_2_T));
  153.   return (place);
  154. }
  155.  
  156. static struct gdb_block *
  157. find (block_number)
  158.      register long int    block_number;
  159. {
  160.   register node_1_T **        pp_1;
  161.   register node_0_T **        pp_0;
  162.   register struct gdb_block *    b;
  163.   register int            index0;
  164.   register int            index1;
  165.   register int            index2;
  166.  
  167. #ifdef SUSPECT
  168.   if (block_number >= BLOCK_NUMBER_LIMIT)
  169.     {
  170.       as_fatal ("gdb_block: Block number = %ld.", block_number);
  171.     }
  172. #endif
  173.  
  174.   index2 = block_number >> (LOG_2_Z0 + LOG_2_Z1);
  175.   index1 = block_number >> (LOG_2_Z0) & ((1 << LOG_2_Z1) - 1);
  176.   index0 = block_number & ((1 << LOG_2_Z0) - 1);
  177.   pp_1 = * root + index2;
  178.   if (* pp_1 == 0)
  179.     {
  180.       * pp_1 = new_1 ();
  181.     }
  182.   pp_0 = ** pp_1 + index1;
  183.   if (* pp_0 == 0)
  184.     {
  185.       * pp_0 = new_0 ();
  186.     }
  187.   b = ** pp_0 + index0;
  188.   return (b);
  189. }
  190.  
  191.  
  192. static struct gdb_block *
  193. find_create (block_number)
  194.      long int    block_number;
  195. {
  196.   if (highest_block_number_seen < block_number)
  197.     {
  198.       highest_block_number_seen = block_number;
  199.     }
  200.   return (find (block_number));
  201. }
  202.  
  203. void
  204. gdb_block_beg (block_number, frag, offset)
  205.      long int    block_number;
  206.      fragS *    frag;
  207.      long int    offset;
  208. {
  209.   struct gdb_block *    pointer;
  210.       
  211.   pointer = find_create (block_number);
  212. #ifdef SUSPECT
  213.   if (pointer -> begin_frag != 0)
  214.     {
  215.       as_warn( "Overwriting begin_frag for block # %ld.", block_number );
  216.     }
  217.   if (pointer -> begin_where_in_frag != 0)
  218.     {
  219.       as_warn( "Overwriting begin_where_in_frag for block # %ld.", block_number );
  220.     }
  221. #endif
  222.   pointer -> begin_frag      = frag;
  223.   pointer -> begin_where_in_frag = offset;
  224. }
  225.  
  226. void
  227. gdb_block_end (block_number, frag, offset)
  228.      long int    block_number;
  229.      fragS *    frag;
  230.      long int    offset;
  231. {
  232.   struct gdb_block *    pointer;
  233.       
  234.   pointer = find_create (block_number);
  235. #ifdef SUSPECT
  236.   if (pointer -> end_frag != 0)
  237.     {
  238.       as_warn( "Overwriting end_frag for block # %ld.", block_number );
  239.     }
  240.   if (pointer -> end_where_in_frag != 0)
  241.     {
  242.       as_warn( "Overwriting end_where_in_frag for block # %ld.", block_number );
  243.     }
  244. #endif
  245.   pointer -> end_frag           = frag;
  246.   pointer -> end_where_in_frag = offset;
  247. }
  248.  
  249. void
  250. gdb_block_position (block_number, pos)
  251.      long int    block_number;
  252.      long int    pos;
  253. {
  254.   struct gdb_block *    pointer;
  255.  
  256.   pointer = find_create (block_number);
  257.   if (pointer -> position != 0)
  258.     {
  259.       as_warn( "Overwriting old position %ld. in block #%ld.",
  260.           pointer -> position, block_number);
  261.     }
  262.   pointer -> position = pos;
  263. }
  264.  
  265. void
  266. gdb_block_emit ()
  267. {
  268.   long int        block_number;
  269.   struct gdb_block *    b;
  270.  
  271.   for (block_number = 0;
  272.        block_number <= highest_block_number_seen;
  273.        block_number ++)
  274.     {
  275.       b = find (block_number);
  276.       if (b -> begin_frag)
  277.     {
  278.       gdb_alter (b -> position,
  279.              (long int)
  280.              (b -> begin_frag -> fr_address + b -> begin_where_in_frag));
  281.     }
  282.       if (b -> end_frag)
  283.     {
  284.       gdb_alter (b -> position + sizeof( long int ),
  285.              (long int)
  286.              (b -> end_frag -> fr_address + b -> end_where_in_frag));
  287.     }
  288.     }
  289. }
  290.  
  291. /* end: gdb_block.c */
  292.